package com.oa.framework.acl.url.domain;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.oa.common.mybatis.typehandler.Boolean2Number;
import com.oa.common.tree.TreeNode;
import com.oa.framework.acl.ResourceType;
import com.oa.framework.acl.SystemResource;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author cuichao
 * 系统url资源
 */
@Getter
@Setter
@TableName(value = "acl_system_resource")
public class SystemUrlResource extends ResourceNode implements TreeNode<SystemUrlResource>, SystemResource<SystemUrlResource> {
    /**
     *父级主键
     */
    @JsonSerialize(using=ToStringSerializer.class)
    @TableField(value = "parent_id")
    private Long parentId;
    /**
     *资源url
     */
    @TableField(value = "resource_pattern")
    private String resourcePattern;
    /**
     *资源类型
     */
    @TableField(value = "resource_type")
    private ResourceType resourceType;
    /**
     * 资源顺序
     */
    @TableField(value = "order_number")
    private int orderNumber;
    /**
     * 父节点
     */
    @TableField(exist = false)
    private SystemUrlResource parent;
    /**
     * 子节点
     */
    @TableField(exist = false)
    private List<SystemUrlResource> children;
    /**
     * API adapter
     */
    @TableField(exist = false)
    private Set<String> permissions;

    @Override
    public boolean hasLeaf() {
        return CollectionUtils.isEmpty(children);
    }

    @Override
    public ResourceType getResourceType() {
        return resourceType;
    }


    @Override
    public String getResource() {
        return resourcePattern;
    }

    @Override
    public Set<String> getPermissions() {
        return CollectionUtils.isEmpty(permissions) ? Collections.singleton(super.getPermission()) :permissions;
    }

    @Override
    public void setChildren(List nodes) {
        this.children = nodes;
    }

    @Override
    public Object getKey() {
        return super.getResourceId();
    }

    @Override
    public String getName() {
        return super.getResourceName();
    }

    @Override
    public Object getParentKey() {
        return parentId;
    }

    @Override
    public SystemUrlResource getParent() {
        return parent;
    }

    @Override
    public void setParent(SystemUrlResource systemUrlResource) {
        this.parent = systemUrlResource;
    }

    @Override
    public String getNodeType() {
        return resourceType.getType();
    }
}

